// src/main/java/org/zachary/creditbusiness/hotel/controller/BanController.java
package org.zachary.creditbusiness.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.zachary.creditbusiness.hotel.entity.Ban;
import org.zachary.creditbusiness.hotel.mapper.BanMapper;

@RestController
@RequestMapping("/hotels")
public class BanController {

    @Autowired
    private BanMapper banMapper;

    /**
     * 恢复用户：删除 creditbusiness_ban 表中所有该 userId 的记录
     */
    @PostMapping("/recover")
    public ResponseEntity<String> recoverUser(@RequestParam Long userId) {
        int deleted = banMapper.delete(
                new QueryWrapper<Ban>().eq("user_id", userId)
        );
        return ResponseEntity.ok("恢复用户 " + userId + "，共删除 "
                + deleted + " 条记录");
    }



    /**
     * 前端调用 /hotels/checkban?userId=xxx
     * 返回 1=已被 Ban，0=正常
     */
    @GetMapping("/checkban")
    public int checkBan(@RequestParam Long userId) {
        int cnt = Math.toIntExact(banMapper.selectCount(
                new QueryWrapper<Ban>().eq("user_id", userId)
        ));
        return cnt > 0 ? 1 : 0;
    }
}